package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AdAuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.AdUserAuthMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.AdUserAuthService;
import com.heima.utils.thread.AdminThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: AdUserAuthImpl
 * Description:
 * date: 2023/8/22 0022 18:52
 *
 * @author zHan
 */
@Service
@Slf4j
public class AdUserAuthServiceImpl extends ServiceImpl<AdUserAuthMapper, ApUserRealname> implements AdUserAuthService {

    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 查询认证列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(AdAuthDto dto) {
        // 检查参数
        dto.checkParam();
        // 用户未登录
        AdUser adUser = AdminThreadLocalUtil.getUser();
        if (adUser == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 分页条件查询
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page, Wrappers.<ApUserRealname>lambdaQuery()
                .eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus())
                .orderByDesc(ApUserRealname::getCreatedTime));
        // 封装对象
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(),
                (int) page.getTotal());
        responseResult.setData(page.getRecords());
        // 结果返回
        return responseResult;
    }

    /**
     * 用户审核成功
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AdAuthDto dto) {
        // 检查参数
        if (dto.getId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 查询用户是否存在
        ApUserRealname apUserRealname = getById(dto.getId());
        if (apUserRealname == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 修改状态
        apUserRealname.setStatus(ApUserRealname.Status.SUCCESS.getCode());
        // 修改操作
        updateById(apUserRealname);
        // 新增app端数据
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        // 设置权限
        apUser.setIdentityAuthentication(true);
        apUserMapper.updateById(apUser);
        // 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AdAuthDto dto) {
        // 检查参数
        if (dto.getId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 查询用户是否存在
        ApUserRealname apUserRealname = getById(dto.getId());
        if (apUserRealname == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 修改状态和拒绝理由
        apUserRealname.setStatus(ApUserRealname.Status.FAIL.getCode());
        apUserRealname.setReason(dto.getMsg());
        // 修改操作
        updateById(apUserRealname);
        // 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
